package com.qire.manhua.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;

import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.constant.Navigation;
import com.qire.common.dal.WarehouseFactoryVo;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.support.base.ProxyViewModel;
import com.qire.common.widget.adapter.auto.SummerBindAdapter;
import com.qire.manhua.R;
import com.qire.manhua.controller.InviteFriendsController;
import com.qire.manhua.dal.ITaskDataDAL;
import com.qire.manhua.databinding.ActivityInviteFriendsBinding;
import com.qire.manhua.model.viewModel.IInviteFriendsViewModel;
import com.qire.manhua.model.viewModel.impl.ShareItemViewModel;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

/**
 * 邀请好友页面
 */
@RouteMapping(path = Navigation.Page.InviteFriendsPage)
public class InviteFriendsActivity extends BaseActivity<ActivityInviteFriendsBinding> {

    @Autowired(injector = ViewModelInjector.class)
    ProxyViewModel<IInviteFriendsViewModel> viewModel;

    @Autowired(injector = EventControllerInjector.class)
    InviteFriendsController controller;

    Observer<String> observer;
    {
        observer = path -> {
            ScrollView scrollView = viewDataBinder().imgScrollView;
            scrollView.postDelayed(() -> scrollView.fullScroll(ScrollView.FOCUS_DOWN), 300);
            viewModel.removeObserver("postersImgUrl", observer);
            observer = null;
        };
    }

    @Override
    public int layoutRid() {
        return R.layout.activity_invite_friends;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        viewModel.set("adapter", new SummerBindAdapter(ShareItemViewModel.buildToInviteFriends(), controller));
        viewModel.set("faceToFaceVisible", View.GONE);

        controller.onAddition(viewModel);

        ITaskDataDAL taskDataDAL = WarehouseFactoryVo.create(viewModel, ITaskDataDAL.class);
        taskDataDAL.fetchInviteFriendsInfo();

        // 观察图片路径设置时，触发滚动效果
        viewModel.observeForever("postersImgUrl", observer);
    }

}
